<?php

namespace app\servershoppingcart\service\shoppingcart;
use app\manage\service\ApiBase;

/**
 * @author:      李颖
 * @DateTime:    2017-10-11 12:15:32
 * @Description: 购物车
 */

class Shoppingcart extends ApiBase
{
	
	protected $shoppingcartLogic = 'app\servershoppingcart\logic\Shoppingcart';
    protected $shoppingcartValidate = 'app\servershoppingcart\validate\Shoppingcart';
    
    /*将某商品加入到购物车*/
    public function addCart(): array 
    {
    	$data = $this->haveIssetReceiveData(['user_id'=>0, 'goods_info'=>[], 'number'=>0, 'goods_attrs'=>[], 'final_price'=>0, 'final_currency_price'=>0]);
    	$validate = validate($this->shoppingcartValidate);
    	if(!$validate->check($data)){  return verify_err(null, $validate->getError()); }
    	if(!$validate->scene('checkGoodsInfo')->check($data['goods_info'])){ return verify_err(null, $validate->getError()); }
        $checkGoodsAttrs = $this->_checkGoodsAttrs($data['goods_attrs']);
        if($checkGoodsAttrs['status'] != SUCCESS_STATUS){
            return $checkGoodsAttrs;
        }
    	if($data['goods_info']['status'] != 1){ return verify_err(null, '该商品不为在售状态'); }
    	return $this->logic($this->shoppingcartLogic)->addCart($data);
    }

    //加入购物车时 检验商品属性的数据
    protected function _checkGoodsAttrs($data): array 
    {
        foreach ($data as $k => $v) {
            if(!isset($v['name'], $v['en_name'], $v['value'])){
                return verify_err(null, '请检查商品属性[' . $v['name'] . ']的参数传入');
            }
        }
        return auto_success($data, '商品属性的参数完整');
    }

    /*将某商品从购物车移除*/
    public function delCartGoods(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'id'=>0]);
        return $this->stepRun($data, 'delCartGoods');
    }

    /*获取我的购物车*/
    public function getMyCart(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0]);
        return $this->stepRun($data, 'getMyCart');
    }

    /*购物车-1*/
    public function decMyCart(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'id'=>0]);
        return $this->stepRun($data, 'decMyCart');
    }

    /*艺货 获取我的购物车中的商品单价变动信息*/
    public function goodsChangePriceInfo(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'goods_ids'=>' ', 'goods_infos'=>[], 'shoppingcart'=>[]]);
        //判断基本数据格式
        $validate = validate($this->shoppingcartValidate);
        if(!$validate->check($data)){  return verify_err(null, $validate->getError()); }
        //判断购物车数据的完整性
        $goodsIdsArray = explode(",", $data['goods_ids']);
        foreach ($data['shoppingcart'] as $k => $v) {
            if(!isset($v['user_id'], $v['goods_id'], $v['goods_info']['price'], $v['goods_info']['orther_price']['currency'], $v['goods_info']['title'], $v['price'], $v['orther_price']['currency'])){ return verify_err(null, '购物车中的商品信息不完整'); }
            if($v['user_id'] != $data['user_id']){ return verify_err(null, '商品[' . $v['goods_info']['title'] . ']不属于你'); }
            if(!in_array($v['goods_id'], $goodsIdsArray)){ return verify_err(null, '购物车中商品id错误'); }
        }
        //判断最新商品信息的完整性
        foreach ($data['goods_infos'] as $k => $v) {
            if(!isset($v['id'], $v['price'], $v['orther_price']['currency'])){ return verify_err(null, '最新商品信息不完整'); }
        }
        //这里可以进行更进一步的判断 判断goods_ids 和 shoppingcart中的商品是否完全对应
        return $this->stepRun($data, 'goodsChangePriceInfo');
    }

    /*艺货 - 从购物车中删除多个商品*/
    public function delCartByIds(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'ids'=>' ']);
        return $this->stepRun($data, 'delCartByIds');
    }



} 